//
//  VistorView.swift
//  Weibo
//
//  Created by 李亚乐 on 15/12/6.
//  Copyright © 2015年 李亚乐. All rights reserved.
//

import UIKit

class VisitorView: UIView {
    
    //外部方法
    func setupInfo(title: String, imageName: String?) {
        
        infoLabel.text = title
        
        guard let imgname = imageName else {
            startAnim()
            return
        }
        
        iconView.image = UIImage(named: imgname)
        houseView.hidden = true
        sendSubviewToBack(maskIconView)
    }
    
    private func startAnim() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        
        // 用在不断重复的动画上，当动画绑定的图层对应的视图被销毁，动画会自动被销毁
        anim.removedOnCompletion = false
        
        // 添加到图层
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    //纯代码
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }

    //SB XIB
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
//        setupUI()
    }
    
    private func setupUI() {
        /// 添加子控件
        addSubview(iconView)
        //遮罩
        addSubview(maskIconView)
        addSubview(houseView)
        addSubview(infoLabel)
        addSubview(logonButton)
        addSubview(registerButton)

        ///布局子控件
        layoutSubview()
        
        ///设置背景颜色与遮罩颜色相同
        backgroundColor = UIColor(white: 237.0 / 255, alpha: 1.0)
    }
    
//MARK: - 懒加载控件
    
    ///转轮
    lazy var iconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    //遮罩
    lazy var maskIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    // 小房子
    lazy var houseView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //消息标签
    lazy var infoLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")
    
    //注册按钮
    lazy var registerButton: UIButton = UIButton(backgroundImageName: "common_button_white_disable", title: "注册", titleColor: UIColor.orangeColor())
    
    //登陆按钮
    lazy var logonButton: UIButton = UIButton(backgroundImageName: "common_button_white_disable", title: "登陆", titleColor: UIColor.darkGrayColor())
    
}

// MARK: - 布局子控件 autolayout
extension VisitorView {
    
    private func layoutSubview() {
        //使用autoresize
        for view in subviews {
            view.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //转轮
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        
        //遮罩
        // 6. 遮罩图像
        /**
        VFL : 可视化格式语言
        
        H 水平方向
        V 垂直方向
        | 边界
        [] 包装控件
        views: 是一个字典 [名字: 控件名] - VFL 字符串中表示控件的字符串
        metrics: 是一个字典 [名字: NSNumber] - VFL 字符串中表示某一个数值
        */
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask": maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(buttonH)-[regbutton]", options: [], metrics: ["buttonH": -36], views: ["mask": maskIconView, "regbutton": registerButton]))
        
        //小房子
        addConstraint(NSLayoutConstraint(item: houseView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseView, attribute: .CenterY , relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        //消息标签
        addConstraint(NSLayoutConstraint(item: infoLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: infoLabel, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: infoLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: infoLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: infoLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: infoLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        //登陆按钮
        addConstraint(NSLayoutConstraint(item: logonButton, attribute: .Right, relatedBy: .Equal, toItem: infoLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: logonButton, attribute: .Top, relatedBy: .Equal, toItem: infoLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: logonButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: logonButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
    }
    
    
}




